package cn.smbms.util;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: laoyu
 * @Date: 2019/9/2 9:41
 * @Description:
 */
public class StringToDateConverter implements Converter<String,Date> {
    //日期的字符串表现形式
    private String datePattern;
    //构造函数
    public StringToDateConverter(String datePattern) {
        System.out.println("StringToDateConverter convert ============== " + datePattern );
        this.datePattern = datePattern;
    }

    @Override
    public Date convert(String s) {
        //定义日期对象
        Date date=null;
        try {
            date=new SimpleDateFormat(datePattern).parse(s);
            System.out.println("StringToDateConverter convert date " +
                    "================================= > " + date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
